<template>
    <div class="home-container">
        <!-- 波浪背景 -->
        <div class="wave-background">
            <div class="wave wave1"></div>
            <div class="wave wave2"></div>
            <div class="wave wave3"></div>
        </div>

        <header class="header" :class="{ scrolled: isScrolled }">
            <div class="logo">
                <div class="logo-icon">
                    <span class="bracket">{</span>
                    <span class="dot">.</span>
                    <span class="bracket">}</span>
                </div>
            </div>
            <h1 class="title">代码演练场</h1>
            <p class="subtitle">在线编写、实验和分享代码</p>
            <div class="description">
                <p class="highlight">探索 Web 开发的无限可能</p>
                <p class="features">
                    实时编辑 HTML、CSS 和 JavaScript，
                    即时预览效果，让编码更加简单有趣
                </p>
                <div class="tech-badges">
                    <span class="badge html">
                        HTML
                    </span>
                    <span class="badge css">
                        CSS
                    </span>
                    <span class="badge js">
                        JavaScript
                    </span>
                </div>
            </div>
        </header>

        <transition-group name="card-list" tag="div" class="card-grid" appear>
            <div v-for="(example, index) in examples" :key="example.id" class="example-card"
                :style="{ animationDelay: `${index * 0.1}s` }" @click="navigateToPlayground(example.id)">
                <div class="card-content">
                    <div class="card-header">
                        <div class="tech-icons">
                            <img v-for="tag in example.tags" :key="tag" :src="getIcon(tag)" :alt="tag"
                                class="tech-svg" />
                        </div>
                        <h3>{{ example.title }}</h3>
                    </div>
                    <p>{{ example.description }}</p>
                    <div class="tags">
                        <span v-for="tag in example.tags" :key="tag" class="tag" :class="tag">
                            {{ tag }}
                        </span>
                    </div>
                    <div class="card-footer">
                        <span class="view-more">查看详情 →</span>
                    </div>
                </div>
                <div class="card-decoration"></div>
            </div>
        </transition-group>

        <div class="feature-grid">
            <div v-for="(feature, index) in features" :key="index" class="feature-card"
                :style="{ transform: `rotate(${-2 + Math.random() * 4}deg)` }">
                <i :class="feature.icon" class="feature-icon"></i>
                <h3>{{ feature.title }}</h3>
                <p>{{ feature.description }}</p>
            </div>
        </div>
    </div>
</template>

<script setup>
import { storeToRefs } from 'pinia'
import { useRouter } from 'vue-router'
import { useExamplesStore } from '../stores/examples'

const router = useRouter()
const examplesStore = useExamplesStore()
const { examples } = storeToRefs(examplesStore)

const navigateToPlayground = (id) => {
    router.push(`/playground/${id}`)
}

const getIcon = (tag) => {
    switch (tag) {
        case 'html': return '/icons/html5.svg';
        case 'css': return '/icons/css3.svg';
        case 'JavaScript': return '/icons/JavaScript.svg';
        case 'vue': return '/icons/vue.svg';
        default: return '/icons/code.svg';
    }
}
</script>

<style>
.home {
    min-height: 100vh;
    padding: var(--space-4);
    background: var(--bg-primary);
    position: relative;
    overflow: hidden;
}

/* 波浪背景动画 */
.wave-background {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 0;
    overflow: hidden;
    background: var(--bg-primary);
}

.wave {
    position: absolute;
    width: 200%;
    height: 200%;
    background: linear-gradient(45deg, var(--primary-color), var(--accent-color));
    opacity: 0.05;
    border-radius: 40%;
}

.wave1 {
    animation: wave 25s infinite linear;
}

.wave2 {
    animation: wave 15s infinite linear;
    opacity: 0.03;
}

.wave3 {
    animation: wave 35s infinite linear;
    opacity: 0.02;
}

@keyframes wave {
    from {
        transform: translate(-50%, -50%) rotate(0deg);
    }

    to {
        transform: translate(-50%, -50%) rotate(360deg);
    }
}

.header {
    text-align: center;
    margin-bottom: var(--space-6);
    padding: var(--space-6) var(--space-4);
    animation: fadeInDown 0.8s ease-out;
    background: linear-gradient(to bottom, var(--bg-secondary), var(--bg-primary));
    border-bottom: 1px solid var(--border-color);
}

.title {
    font-size: 48px;
    color: var(--text-primary);
    margin-bottom: var(--space-3);
    font-weight: 600;
    letter-spacing: -1px;
    background: linear-gradient(45deg, var(--primary-color), var(--accent-color));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
}

.subtitle {
    font-size: var(--text-xl);
    color: var(--text-secondary);
    margin-bottom: var(--space-4);
    font-weight: 500;
}

.description {
    max-width: 600px;
    margin: 0 auto;
}

.highlight {
    font-size: var(--text-xl);
    color: var(--primary-color);
    margin-bottom: var(--space-3);
    font-weight: 500;
}

.features {
    color: var(--text-secondary);
    line-height: 1.6;
    margin-bottom: var(--space-4);
}

.tech-badges {
    display: flex;
    gap: var(--space-2);
    justify-content: center;
    margin-top: var(--space-4);
}

.badge {
    padding: 6px 12px;
    border-radius: var(--radius-full);
    font-size: var(--text-sm);
    font-weight: 500;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    animation: fadeInUp 0.5s ease-out both;
}

.badge.html {
    background: #E44D26;
    color: white;
    animation-delay: 0.2s;
}

.badge.css {
    background: #264DE4;
    color: white;
    animation-delay: 0.3s;
}

.badge.js {
    background: #F0DB4F;
    color: #323330;
    animation-delay: 0.4s;
    text-transform: capitalize;
}

.examples-container {
    max-width: 1400px;
    margin: 0 auto;
    padding: var(--space-4);
}

.examples-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    gap: var(--space-4);
}

.example-card {
    position: relative;
    background: var(--bg-secondary);
    border-radius: var(--radius-lg);
    overflow: hidden;
    cursor: pointer;
    transition: all 0.3s var(--ease-out);
    border: 1px solid var(--border-color);
    animation: fadeInUp 0.5s ease-out both;
}

.example-card::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 4px;
    background: linear-gradient(90deg, var(--primary-color), var(--accent-color));
    opacity: 0;
    transition: opacity 0.3s ease;
}

.example-card:hover::before {
    opacity: 1;
}

.example-card:hover {
    transform: translateY(-4px);
    box-shadow: 0 12px 24px -10px rgba(0, 0, 0, 0.3);
    border-color: var(--primary-color);
}

.card-content {
    padding: var(--space-2);
}

.card-content h3 {
    font-size: var(--text-xl);
    color: var(--text-primary);
    margin-bottom: var(--space-2);
    font-weight: 500;
}

.card-content p {
    color: var(--text-secondary);
    margin-bottom: var(--space-3);
    line-height: 1.5;
}

.tags {
    display: flex;
    gap: var(--space-2);
    flex-wrap: wrap;
}

.tag {
    padding: 4px 8px;
    border-radius: var(--radius-sm);
    font-size: var(--text-xs);
    font-weight: 500;
    text-transform: uppercase;

    &:nth-child(3) {
        text-transform: capitalize;
    }
}

.tag.html {
    background: #E44D26;
    color: white;
}

.tag.css {
    background: #264DE4;
    color: white;
}

.tag.JavaScript {
    background: #F0DB4F;
    color: #323330;
    text-transform: capitalize;
}

.tag.vue {
    background: #41B883;
    color: white;
}

.card-header {
    display: flex;
    align-items: center;
    gap: 0.8rem;
    margin-bottom: 0.7rem;
}

.card-icon {
    width: 38px;
    height: 38px;
    border-radius: 10px;
    background: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 2px 8px 0 rgba(58, 86, 138, 0.10);
    padding: 4px;
}

.tech-icons {
    display: flex;
    gap: 0.3rem;
}

.tech-svg {
    width: 22px;
    height: 22px;
    display: block;
    border-radius: 4px;
    background: #fff;
    box-shadow: 0 1px 4px 0 rgba(58, 86, 138, 0.08);
    padding: 2px;
}

.card-content h3 {
    color: #fff;
    font-size: 1.1rem;
    font-weight: 600;
    margin: 0 0 0.2em 0;
    flex: 1;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.card-content p {
    color: #bfc8e6;
    font-size: 0.98rem;
    margin: 0 0 0.5rem 0;
    line-height: 1.5;
}

.tags {
    display: flex;
    gap: 0.4rem;
    flex-wrap: wrap;
    margin-top: 0.2rem;
}

.tag {
    padding: 0.15em 0.7em;
    border-radius: 999px;
    font-size: 0.82em;
    font-weight: 500;
    background: rgba(255, 255, 255, 0.10);
    color: #fff;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

.card-footer {
    margin-top: var(--space-3);
    text-align: right;
}

.view-more {
    color: var(--primary-color);
    font-size: var(--text-sm);
    font-weight: 500;
    opacity: 0;
    transform: translateX(-10px);
    transition: all 0.3s ease;
}

.example-card:hover .view-more {
    opacity: 1;
    transform: translateX(0);
}

/* 动画 */
@keyframes fadeInUp {
    from {
        opacity: 0;
        transform: translateY(20px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

@keyframes fadeInDown {
    from {
        opacity: 0;
        transform: translateY(-20px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* 列表动画 */
.list-enter-active,
.list-leave-active {
    transition: all 0.5s ease;
}

.list-enter-from {
    opacity: 0;
    transform: translateY(30px);
}

.list-leave-to {
    opacity: 0;
    transform: translateY(-30px);
}

/* 响应式设计 */
@media (max-width: 768px) {
    .examples-grid {
        grid-template-columns: 1fr;
    }

    .home {
        padding: var(--space-3);
    }

    .header {
        margin-bottom: var(--space-4);
        padding: var(--space-4);
    }

    .header h1 {
        font-size: var(--text-xl);
    }

    .subtitle {
        font-size: var(--text-base);
    }

    .title {
        font-size: 36px;
    }

    .highlight {
        font-size: var(--text-lg);
    }

    .wave {
        display: none;
    }
}

@media (min-width: 769px) and (max-width: 1024px) {
    .examples-grid {
        grid-template-columns: repeat(2, 1fr);
    }
}

@media (min-width: 1025px) and (max-width: 1440px) {
    .examples-grid {
        grid-template-columns: repeat(3, 1fr);
    }
}

@media (min-width: 1441px) {
    .examples-grid {
        grid-template-columns: repeat(4, 1fr);
    }
}

/* 技术图标 */
.tech-icon {
    display: inline-block;
    width: 20px;
    height: 20px;
    margin-right: 6px;
    background-size: contain;
    background-repeat: no-repeat;
    vertical-align: middle;
}

.html-icon {
    background-image: url('/icons/html5.svg');
}

.css-icon {
    background-image: url('/icons/css3.svg');
}

.js-icon {
    background-image: url('/icons/JavaScript.svg');
}

.card-grid {
    background: linear-gradient(120deg, #232a3d 0%, #2d225a 100%);
    border-radius: 18px;
    box-shadow: 0 8px 32px 0 rgba(58, 86, 138, 0.18);
    padding: 2rem 1.2rem;
    margin: 0 auto;
    max-width: 1400px;
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
    gap: 1.1rem;
}

.example-card {
    background: rgba(34, 40, 60, 0.96);
    border-radius: 14px;
    box-shadow: 0 2px 12px 0 rgba(58, 86, 138, 0.10);
    border: 1.5px solid rgba(107, 64, 227, 0.10);
    transition: box-shadow 0.22s, transform 0.22s, border 0.22s;
    min-height: 150px;
    cursor: pointer;
    padding: 0;
}



.card-content {
    padding: 1.1rem 1rem 0.9rem 1rem;
    display: flex;
    flex-direction: column;
    gap: 0.7rem;
}

.card-content h3 {
    color: #fff;
    font-size: 1.1rem;
    font-weight: 600;
    margin: 0 0 0.2em 0;
}

.card-content p {
    color: #bfc8e6;
    font-size: 0.98rem;
    margin: 0 0 0.5rem 0;
    line-height: 1.5;
}

.tags {
    display: flex;
    gap: 0.4rem;
    flex-wrap: wrap;
}

.tag {
    padding: 0.15em 0.7em;
    border-radius: 999px;
    font-size: 0.82em;
    font-weight: 500;
    background: rgba(255, 255, 255, 0.10);
    color: #fff;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

.tag.html {
    background: #e44d26cc;
}

.tag.css {
    background: #264de4cc;
}

.tag.JavaScript {
    background: #f0db4fcc;
    color: #323330;
}

.tag.vue {
    background: #41b883cc;
}

.card-footer {
    margin-top: 0.3rem;
    text-align: right;
}

.view-more {
    color: #fff;
    font-size: 0.95em;
    font-weight: 500;
    opacity: 0.7;
    transition: opacity 0.2s;
}

.example-card:hover .view-more {
    opacity: 1;
}

@media (max-width: 768px) {
    .card-grid {
        grid-template-columns: 1fr;
        padding: 1rem 0.3rem;
        gap: 0.6rem;
    }

    .card-content {
        padding: 0.7rem 0.6rem 0.6rem 0.6rem;
    }
}
</style>

<style scoped lang="scss">
.home-container {
    background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
    min-height: 100vh;
    position: relative;
    overflow: hidden;

    &::before {
        content: '';
        position: absolute;
        width: 200%;
        height: 200%;
        top: -50%;
        left: -50%;
        background: radial-gradient(circle, rgba(255, 255, 255, 0.8) 0%, rgba(255, 255, 255, 0) 70%);
        animation: rotate 25s linear infinite;
        z-index: 0;
    }
}

.header {
    backdrop-filter: blur(10px);
    transition: background-color 0.3s;

    &.scrolled {
        background: rgba(255, 255, 255, 0.95);
    }
}



.feature-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
    gap: 2rem;
    padding: 2rem;

    .feature-card {
        background: linear-gradient(145deg, #2a2a2a, #1a1a1a);
        border: 1px solid rgba(255, 255, 255, 0.1);
        border-radius: 12px;
        padding: 1.5rem;
        transition: all 0.3s ease;
        box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);

        &:hover {
            transform: translateY(-5px) rotate(0deg) !important;
            background: linear-gradient(145deg, #333, #222);
            border-color: rgba(255, 255, 255, 0.2);
        }

        .feature-icon {
            font-size: 2.5rem;
            margin-bottom: 1rem;
            color: #61dafb;
            transform: rotate(-10deg);
            display: inline-block;
            text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
        }

        h3 {
            color: #fff;
            margin-bottom: 0.5rem;
            font-size: 1.4rem;
        }

        p {
            color: #b3b3b3;
            font-size: 0.95rem;
            line-height: 1.5;
        }
    }
}

@keyframes rotate {
    from {
        transform: rotate(0deg);
    }

    to {
        transform: rotate(360deg);
    }
}

.card-list-enter-active,
.card-list-leave-active {
    transition: all 0.5s ease;
}

.card-list-enter-from,
.card-list-leave-to {
    opacity: 0;
    transform: translateY(30px);
}

.card-list-move {
    transition: transform 0.5s ease;
}

::-webkit-scrollbar {
    width: 8px;
    height: 8px;
}

::-webkit-scrollbar-track {
    background: rgba(0, 0, 0, 0.1);
    border-radius: 4px;
}

::-webkit-scrollbar-thumb {
    background: rgba(0, 0, 0, 0.2);
    border-radius: 4px;

    &:hover {
        background: rgba(0, 0, 0, 0.3);
    }
}
</style>